package com.hp.controller;

import com.github.pagehelper.PageInfo;
import com.hp.bean.Customerservice;
import com.hp.bean.CustomerserviceExample;
import com.hp.service.CustomerserviceService;
import com.hp.util.JsonData;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/customerservice")
public class CustomerserviceController {
    @Autowired
    private CustomerserviceService service;
    //客服登录
    @RequestMapping("/cslogin")
    @ResponseBody
    public Object customerservicelogin(CustomerserviceExample customerservice,String code, HttpSession session) {
        Map<String,Object> map = new HashMap<>();
        String codeStr = (String) session.getAttribute("code");
        if (codeStr == null || !codeStr.equalsIgnoreCase(code)) {
            map.put("type", "error");
            map.put("msg", "验证码错误！");
        }
        Customerservice cs = service.login(customerservice);
        System.out.println(cs);
        if(cs!=null){
            System.out.println("true");
            session.setAttribute("customerservice", cs);
            map.put("type", "success");
            map.put("msg", "登录成功！");
            return map;
        }else{
            System.out.println("false");
            map.put("type", "error");
            map.put("msg", "登录失败！");
            return map;
        }
    }

    @RequestMapping("/find")
    @ResponseBody
    public Object find(String name,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "5") Integer limit) {
        CustomerserviceExample example = new CustomerserviceExample();
        CustomerserviceExample.Criteria criteria = example.createCriteria();
        if(!StringUtils.isNullOrEmpty(name)){
            criteria.andNameEqualTo(name);
        }
        //未具体实现分页的写法


        PageInfo<Customerservice> list =service.pageFind(example, page, limit);
        return JsonData.buildSuccess(list);
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Object findById(Integer id) {
        Customerservice customerservice = service.findByid(id);
        return JsonData.buildSuccess(customerservice);
    }
    @RequestMapping("/add")
    @ResponseBody
    public Object add(Customerservice customerservice) {
        int i = service.add(customerservice);
        return i==1? JsonData.buildSuccess("添加成功！"): JsonData.buildError("添加失败");
    }
    @RequestMapping("/update")
    @ResponseBody
    public Object update(Customerservice customerservice) {
        int i = service.upp(customerservice);
        return i==1? JsonData.buildSuccess("修改成功！"): JsonData.buildError("修改失败");

    }
    @RequestMapping("/del")
    @ResponseBody
    public Object del(Integer id) {
        int i = service.del(id);
        return i==1? JsonData.buildSuccess("删除成功！"): JsonData.buildError("删除失败");

    }
}
